package com.hthy.futures.ui.fragment;

import android.view.View;

import com.hthy.futures.R;
import com.hthy.futures.adapter.HistoryDataCommonAdapter;
import com.hthy.futures.api.ST_TradeApi;
import com.hthy.futures.base.BaseListFragment;
import com.hthy.futures.base.CommonAdapter;
import com.hthy.futures.bean.HistoryData;
import com.hthy.futures.utils.ErrorUtils;
import com.hthy.futures.utils.LogUtils;
import com.hthy.futures.view.EmptyLayout;
import com.vilyever.socketclient.SocketClient;

import net.hthy.bean.Trade;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import cn.bingoogolapple.refreshlayout.BGARefreshLayout;

/**
  * 资金流水
  * Author:  Mr.bingley
  * Version:
  * Date:  2017/3/6
  */

public class MoneyStreamFragment extends BaseListFragment {


    private CommonAdapter<HistoryData> mHistoryDataCommonAdapter;

    @Override
    public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout bgaRefreshLayout) {
        super.onBGARefreshLayoutBeginRefreshing(bgaRefreshLayout);
        getHistoryMoneyStreamData();
    }

    @Override
    protected void initWidget(View root) {
        super.initWidget(root);
        getHistoryMoneyStreamData();

        mHistoryDataCommonAdapter= new HistoryDataCommonAdapter(getActivity(), R.layout.adapter_moneystream);
        listView.setAdapter(mHistoryDataCommonAdapter);
    }


    /**
     * 得到历史资金数据
     */
    private void getHistoryMoneyStreamData() {
        byte[] bytes = ST_TradeApi.searchGoods("2007");
        socketClient.sendData(bytes);
    }

    @Override
    protected void onResponse(SocketClient client, String responMes, byte[] subBytes) {
        super.onResponse(client, responMes, subBytes);
        LogUtils.e(responMes);

        try {
            Trade.QueryUserFundChangesResponseMsg queryUserFundChangesResponseMsg = Trade.QueryUserFundChangesResponseMsg.parseFrom(subBytes);
            if (queryUserFundChangesResponseMsg != null) {
                int iErrorCode = queryUserFundChangesResponseMsg.getResponseMsg().getIErrorCode();
                if (0 == iErrorCode) {
                    endResponse();
                    List<Trade.UserFundChanges> userFundChangesList = queryUserFundChangesResponseMsg.getUserFundChangesList();
                    if (userFundChangesList != null && userFundChangesList.size() == 0) {
                        errorLayout.setErrorType(EmptyLayout.NODATA);
                        return;
                    } else {
                        List<HistoryData> datas = new ArrayList<>();
                        for (Trade.UserFundChanges userFundChanges : userFundChangesList) {
                            HistoryData historyData = new HistoryData();
                            historyData.setUiChangeType(userFundChanges.getUiChangeType());
                            historyData.setSsExecuteTime(userFundChanges.getSsExecuteTime());
                            historyData.setDbChangeAmount(userFundChanges.getDbChangeAmount()+"");
                            historyData.setDbBalance(userFundChanges.getDbBalance()+"");
                            historyData.set_$8115(userFundChanges.getSsUsername());   // TODO
                            datas.add(historyData);
                        }
                        if (mHistoryDataCommonAdapter.getDatas().size() > 0) {
                            mHistoryDataCommonAdapter.clear();
                        }
                        Collections.reverse(datas);
                        mHistoryDataCommonAdapter.addItem(datas);
                    }

                } else {
                    ErrorUtils.sendErrorMes(getActivity(),iErrorCode + "");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            getHistoryMoneyStreamData();
        }

    }


}
